﻿var userProfileModal = $('#user-profile-modal');

var UserProfile = function(constructors) {

	var self = this;

	self.UserID = 0;

	self.UserName = '';

	self.FullName = '';

	self.Designation = '';

	self.Department = '';

	self.Email = '';

	self.Loading = false;

	self.init = function() {
		var defaults = {
			UserID: 0,
			UserName: '',
			FullName: '',
			Designation: '',
			Department: '',
			Email: ''
		};

		$.extend(defaults, constructors);

		self.UserID = ko.observable(parseInt(defaults.UserID));
		self.UserName = ko.observable(defaults.UserName);
		self.FullName = ko.observable(defaults.FullName);
		self.Designation = ko.observable(defaults.Designation);
		self.Department = ko.observable(defaults.Department);
		self.Email = ko.observable(defaults.Email);
		self.Loading = ko.observable(false);

		self.UserName.hasError = ko.observable(false);
		self.UserName.valid = ko.observable(true);
		self.UserName.errorMessage = ko.observable('');

		self.FullName.hasError = ko.observable(false);
		self.FullName.valid = ko.observable(true);
		self.FullName.errorMessage = ko.observable('');

		self.Designation.hasError = ko.observable(false);
		self.Designation.valid = ko.observable(true);
		self.Designation.errorMessage = ko.observable('');

		self.Department.hasError = ko.observable(false);
		self.Department.valid = ko.observable(true);
		self.Department.errorMessage = ko.observable('');

		self.Email.hasError = ko.observable(false);
		self.Email.valid = ko.observable(true);
		self.Email.errorMessage = ko.observable('');
	}

	self.ClearErrors = function() {
		self.UserName.hasError(false);
		self.UserName.valid(true);

		self.FullName.hasError(false);
		self.FullName.valid(true);

		self.Designation.hasError(false);
		self.Designation.valid(true);

		self.Department.hasError(false);
		self.Department.valid(true);

		self.Email.hasError(false);
		self.Email.valid(true);
	}

	self.Update = function() {
		if (self.Loading()) return;
		self.Loading(true);

		var accountDetail = {
			UserID: self.UserID(),
			UserName: self.UserName(),
			FullName: self.FullName(),
			Designation: self.Designation(),
			Department: self.Department(),
			Email: self.Email()
		};

		try {
			console.info(accountDetail);
		}
		catch (ex) {

		}

		var url = app.baseUrl() + 'account/update';

		$.ajax({
			'url': url,
			type: 'POST',
			contentType: 'application/json',
			dataType: 'json',
			data: JSON.stringify(accountDetail),
			async: false
		}).done(function(response) {
			try {
				console.info(response);
			}
			catch (ex) {

			}

			self.ClearErrors();

			if (!response.Successful) {
				var withSpecificFieldError = false;

				$.each(response.Errors, function(index) {
					var error = this;
					if (error.For != '') {
						withSpecificFieldError = true;
						
						if (error.For.toLowerCase() == 'username') {
							self.UserName.hasError(true);
							self.UserName.valid(false);
							self.UserName.errorMessage(error.Message);
						}
						else if (error.For.toLowerCase() == 'fullrname') {
							self.FullName.hasError(true);
							self.FullName.valid(false);
							self.FullName.errorMessage(error.Message);
						}
						else if (error.For.toLowerCase() == 'designation') {
							self.Designation.hasError(true);
							self.Designation.valid(false);
							self.Designation.errorMessage(error.Message);
						}
						else if (error.For.toLowerCase() == 'department') {
							self.Department.hasError(true);
							self.Department.valid(false);
							self.Department.errorMessage(error.Message);
						}
						else if (error.For.toLowerCase() == 'email') {
							self.Email.hasError(true);
							self.Email.valid(false);
							self.Email.errorMessage(error.Message);
						}
						else { }
					}
				});

				if (!withSpecificFieldError) {
					notification.shout({
						title: 'User Profile Update',
						text: 'Failed to update your user profile with the provided information.',
						image: 'error'
					});
				}
			}
			else {
				notification.shout({
					title: 'User Profile Update',
					text: 'Your profile has been updated.',
					image: 'info'
				});

				$('.user-full-name').text(self.FullName());
				$('.user-name').text(self.UserName());
			}
		}).fail(function(response) {
			try {
				console.error(response);
			}
			catch (ex) {

			}

			notification.shout({
				title: 'User Profile Update',
				text: 'Failed to update your user profile with the provided information.',
				image: 'error'
			});
		}).always(function(response) {
	
		});

		self.Loading(false);
	}

	self.init();

}

var changePasswordModal = $('#change-password-modal');

var UserPassword = function(constructors) {

	var self = this;

	self.UserID = 0;

	self.CurrentPassword = '';

	self.NewPassword = '';

	self.ValidatingPassword = '';

	self.Loading = false;

	self.init = function() {
		var defaults = {
			UserID: 0,
			CurrentPassword: '',
			NewPassword: '',
			ValidatingPassword: ''
		};

		$.extend(defaults, constructors);

		self.UserID = ko.observable(defaults.UserID);
		self.CurrentPassword = ko.observable(defaults.CurrentPassword);
		self.NewPassword = ko.observable(defaults.NewPassword);
		self.ValidatingPassword = ko.observable(defaults.ValidatingPassword);

		self.Loading = ko.observable(false);

		self.CurrentPassword.hasError = ko.observable(false);
		self.CurrentPassword.valid = ko.observable(true);
		self.CurrentPassword.errorMessage = ko.observable('');

		self.NewPassword.hasError = ko.observable(false);
		self.NewPassword.valid = ko.observable(true);
		self.NewPassword.errorMessage = ko.observable('');
		
		self.ValidatingPassword.hasError = ko.observable(false);
		self.ValidatingPassword.valid = ko.observable(true);
		self.ValidatingPassword.errorMessage = ko.observable('');
	}

	self.init();

	self.ClearErrors = function() {
		self.CurrentPassword.hasError(false);
		self.CurrentPassword.valid(true);
		self.CurrentPassword.errorMessage('');

		self.NewPassword.hasError(false);
		self.NewPassword.valid(true);
		self.NewPassword.errorMessage('');
		
		self.ValidatingPassword.hasError(false);
		self.ValidatingPassword.valid(true);
		self.ValidatingPassword.errorMessage('');
	}

	self.Update = function() {
		if (self.Loading()) return;
		self.Loading(true);

		var userPassword = {
			UserID: self.UserID(),
			CurrentPassword: self.CurrentPassword(),
			NewPassword: self.NewPassword(),
			ValidatingPassword: self.ValidatingPassword()
		};

		try {
			console.info(userPassword);
		}
		catch (ex) {

		}

		var url = app.baseUrl() + 'account/changepassword';

		$.ajax({
			'url': url,
			type: 'POST',
			contentType: 'application/json',
			dataType: 'json',
			data: JSON.stringify(userPassword),
			async: false
		}).done(function(response) {
			try {
				console.info(response);
			}
			catch (ex) {

			}

			self.ClearErrors();

			if (!response.Successful) {
				var withSpecificFieldError = false;

				$.each(response.Errors, function(index) {
					var error = this;

					if (error.For != '') {
						withSpecificFieldError = true;

						if (error.For.toLowerCase() == 'currentpassword') {
							self.CurrentPassword.hasError(true);
							self.CurrentPassword.valid(false);
							self.CurrentPassword.errorMessage(error.Message);
						}
						else if (error.For.toLowerCase() == 'newpassword') {
							self.NewPassword.hasError(true);
							self.NewPassword.valid(false);
							self.NewPassword.errorMessage(error.Message);
						}
						else if (error.For.toLowerCase() == 'validatingpassword') {
							self.ValidatingPassword.hasError(true);
							self.ValidatingPassword.valid(false);
							self.ValidatingPassword.errorMessage(error.Message);
						}
						else { }
					}
				});

				if (!withSpecificFieldError) notification.shout({
												title: 'Change Password',
												text: 'Failed to update your password',
												image: 'error'
											});
			}
			else notification.shout({
					title: 'Change Password',
					text: 'Password has been updated.',
					image: 'info'
				});
		}).fail(function(response) {
			try {
				console.error(response);
			}
			catch (ex) {

			}

			notification.shout({
				title: 'Change Password',
				text: 'Failed to update your password.',
				image: 'error'
			});
		}).always(function(response) {

		});

		self.Loading(false);
	}

}

var View = function () {

	var self = this;

	self.init = function() {
		var userProfile = new UserProfile({
			UserID: $('.user-id-field').text(),
			UserName: $('#inputUsername').val(),
			FullName: $('#inputFullName').val(),
			Designation: $('#inputDesignation').val(),
			Department: $('#inputDepartment').val(),
			Email: $('#inputEmail').val()
		});

		self.userProfile = userProfile;
		
		var userPassword = new UserPassword({
			UserID: $('.user-id-field').text()
		});

		self.userPassword = userPassword;
	}

	self.init();

	self.changePasswordMenu = function() {
		self.userPassword.ClearErrors();
		self.userPassword.CurrentPassword('');
		self.userPassword.NewPassword('');
		self.userPassword.ValidatingPassword('');
		changePasswordModal.modal('show');
	}

	self.updateProfileMenu = function() {
		var url = app.baseUrl() + 'account/current';

		$.ajax({
			'url': url,
			type: 'GET',
			dataType: 'json',
			async: false
		}).done(function(response) {
			try {
				console.info(response);
			}
			catch (ex) {

			}

			if (response.User != undefined) {
				if (response.User.UserID != undefined) {
					self.userProfile.ClearErrors();
					self.userProfile.UserName(response.User.UserName);
					self.userProfile.FullName(response.User.FullName);
					self.userProfile.Designation(response.User.Designation);
					self.userProfile.Department(response.User.Department);
					self.userProfile.Email(response.User.Email);
				}
			}
		}).fail(function(response) {
			try {
				console.error(response);
			}
			catch (ex) {

			}
		}).always(function(response) {

		});

		userProfileModal.modal('show');
	}

}

var currentView = new View();